package com.zqwh.xtool.model.online;

import android.content.Context;

import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.api.offline.LendOrderDetailResposity;
import com.zqwh.xtool.api.offline.LendOrderResposity;
import com.zqwh.xtool.api.online.LendOrderApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.LendInfo;
import com.zqwh.xtool.domain.LentData;
import com.zqwh.xtool.domain.StockVO;
import com.zqwh.xtool.greendao.db.LendOrder;
import com.zqwh.xtool.greendao.db.LendOrderDetail;
import com.zqwh.xtool.model.LendOrderModel;

import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.zqwh.xtool.ToolsApp.gUser;

public class LendOrderModelOnlineImpl implements LendOrderModel {
    private LendOrderApi lendOrderApi;
    private Context context;
    private LendOrderResposity lendOrderResposity;
    private LendOrderDetailResposity lendOrderDetailResposity;

    public LendOrderModelOnlineImpl(Context context) {
        this.context = context;
        this.lendOrderApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(LendOrderApi.class);
        this.lendOrderResposity = new LendOrderResposity(this.context);
        this.lendOrderDetailResposity = new LendOrderDetailResposity(this.context);
    }

    @Override
    public void getOwingStock(Long devId, Long userId, NetCallback<LentData> callback) {
        lendOrderApi.getOwingStock("Bearer "+gUser.getToken(), devId)
                .map(new HttpResultFunc2<LentData>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getPreSubmitInfo(Long devId, Long userId, String epcs, NetCallback<LendInfo> callback) {
        lendOrderApi.getPreSubmitInfo("Bearer "+gUser.getToken(), devId, epcs)
                .map(new HttpResultFunc2<LendInfo>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void submitLendReturnOrder(Long devId, Long userId, String epcs, NetCallback<Object> callback) {
        lendOrderApi.submitLendReturnOrder("Bearer "+gUser.getToken(), devId, epcs)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void submitLendOrder(Long devId, Long userId, String orderNo, String stockIds, NetCallback<Object> callback) {
        lendOrderApi.submitLendOrder("Bearer "+gUser.getToken(), devId, userId, orderNo, stockIds)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void submitReturnOrder(Long devId, Long userId, String stockIds, NetCallback<Object> callback) {
        lendOrderApi.submitReturnOrder("Bearer "+gUser.getToken(), devId, userId, stockIds)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getLentOrderDetailList(Long devId, Long userId, NetCallback<HashMap<String, List<StockVO>>> callback) {
        lendOrderApi.getLentOrderDetailList("Bearer "+gUser.getToken(), devId, userId)
                .map(new HttpResultFunc2<HashMap<String, List<StockVO>>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));

    }

    @Override
    public boolean insertMultLendOrder(List<LendOrder> lendOrderList) {
        return lendOrderResposity.insertMultLendOrder(lendOrderList);
    }

    @Override
    public boolean insertMultLendOrderDetail(List<LendOrderDetail> lendOrderDetailList) {
        return lendOrderDetailResposity.insertMultLendOrderDetail(lendOrderDetailList);
    }
}
